<?php
/**
**2015-09-10 对接畅游通切换二次交互模式
**User By  PZP
**/
include('/var/www/html/new/d/module/api/ChangYouTong/cyt.class.php');
include('/var/www/html/new/d/module/api/function.class.php');

$ac='16ucom';
$pw='c33367701511b4f6020ec61ded352059';
$inSide = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

$txt=json_encode($_REQUEST);
logfile($txt,'cyt_get_data');
$PFT_infunction = new AllFunction(); 

if(!isset($_REQUEST['Action']) && $_REQUEST['PayStatus'] == 1){	

	if(!$_REQUEST['TicketID']) exit("101");

	$tid = $_REQUEST['TicketID'] + 0; //门票id，根据门票id获取航线及票类
	// $select_uuid = "select uuid,pid,title,landid from uu_jq_ticket where id=$tid limit 1";
	$select_uuid = "select t.uuid,t.pid,t.title,t.landid,l.apply_did,m.account from uu_jq_ticket as t, uu_land as l,pft_member as m where l.id = t.landid and t.id=$tid and l.apply_did=m.id limit 1";
	$GLOBALS['le']->query($select_uuid);
	if(!$GLOBALS['le']->fetch_assoc())	exit("101");		
	$uuid = $GLOBALS['le']->f('uuid');  //查询票类id	
	$pid = $GLOBALS['le']->f('pid');  
	$productname = $GLOBALS['le']->f('title'); 
	$lid = $GLOBALS['le']->f('landid');  
	$apply_did = $GLOBALS['le']->f('apply_did');  
	$account = $GLOBALS['le']->f('account'); 
	
	$cyt = new ChangYouTong($lid); 
	
	$get_lsprice = $PFT_infunction->Dynamic_Price_And_Storage($account,$pid,substr($_REQUEST['PlayDate'],0,10),1,$apply_did);
	$get_lsprice_xml = simplexml_load_string($get_lsprice);
	$UUlprice = (string)$get_lsprice_xml->Rec->UUsprice;

	$ls_order = 'pft'.str_replace('.',mt_rand(1000,9999),microtime(true));	
	$request_array = array(
		'UUordertime'	=> date('Y-m-d H:i:s'),
		'pftordernum' 	=> $ls_order,
		'uuid'			=> $uuid,	
		'pftordername' 	=> $_REQUEST['TouristName'],
		'UUordertel'	=> $_REQUEST['TouristTel'],
		'IDcar'			=> $_REQUEST['IdCard'],
		'UUtnum' 		=> $_REQUEST['Tnum'],
		'productname'	=> $productname,
		'UUlprice'		=> $UUlprice,
		'UUplaytime'	=> substr($_REQUEST['PlayDate'],0,10)
	);

	$request = $cyt->createPaymentOrder($request_array);
	if($request == 101) exit("101|0|0|0|0|0");
	else{			
		exit("200|0|0|0|0|".(string)$request.'_'.$ls_order);
	}
	
}elseif($_REQUEST['Action'] == 'Relation'){ //下单成功

	$Ordern = $_REQUEST['Ordern'];
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);
	$tnum = (int)$pft_order_xml->Rec->UUtnum;
	$tordernums = explode('_',(string)$pft_order_xml->Rec->UUtordernum);
	$apiOrder = $tordernums['0'];
	$tempOrder = $tordernums['1'];

	$str="insert into all_api_order (pftOrder,apiOrder,tempOrder,coopB,oStatus,oStnum,apiCode,handleStatus,cTime) values('$Ordern','$apiOrder','$tempOrder',3,0,$tnum,'$apiOrder',0,now())";
	$GLOBALS['le']->query($str);
	
}elseif($_REQUEST['Action'] == 'MOD'){//取消订单
	// if($_REQUEST['Ordern'] ==  '1553521') exit("200");
	$modify_array = array();
	
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);	
	$UUtnum = (int)$pft_order_xml->Rec->UUtnum;
	$UUpid = (int)$pft_order_xml->Rec->UUpid;
	$lid = (int)$pft_order_xml->Rec->UUlid;
	$UUplaytime = (string)$pft_order_xml->Rec->UUbegintime;
	$UUtordernum = explode("_",(string)$pft_order_xml->Rec->UUtordernum);
	$UUaids=(string)$pft_order_xml->Rec->UUaids;
	if($UUaids == 0) $UUaid = (int)$pft_order_xml->Rec->UUaid;
	else{
		$UUaidss= explode(',',$UUaids);
		$UUaid = $UUaidss[0]; 
	}
	$sql="select account from pft_member where id=".$UUaid." limit 1";
	$GLOBALS['le']->query($sql);
	$GLOBALS['le']->fetch_assoc(); 
	$account=$GLOBALS['le']->f('account');

	$get_lsprice = $PFT_infunction->Dynamic_Price_And_Storage($account,$UUpid,$UUplaytime,1,$UUaid);
	$get_lsprice_xml = simplexml_load_string($get_lsprice);
	$UUlprice = (string)$get_lsprice_xml->Rec->UUsprice;

	$modify_array['APIOrdern'] = $UUtordernum[0];
	$modify_array['orderRefundCharge'] = 0 ;
	$modify_array['refundExplain'] = "";
	$modify_array['orderPrice'] = $UUtnum * $UUlprice;
	$modify_array['orderQuantity'] = $UUtnum ;
	$modify_array['rectime'] = date('Y-m-d H:i:s');
	$modify_array['refundSeq'] = time();
	$modify_array['name'] = (string)$pft_order_xml->Rec->UUordername;
	$modify_array['idcard'] = (string)$pft_order_xml->Rec->UUpersonid;
		
	if($_REQUEST['Tnum'] == 0){ //取消
		$modify_array['refundQuantity'] = $UUtnum;
		$modify_array['orderRefundPrice'] = $UUtnum * $UUlprice;						
	}else{ //修改
		$modify_array['refundQuantity'] = $UUtnum - $_REQUEST['Tnum'];
		$modify_array['orderRefundPrice'] = ($UUtnum- $_REQUEST['Tnum']) * $UUlprice;
	}		
		
	$cyt = new ChangYouTong($lid);
	$result = $cyt->applyOrderRefundByUser($modify_array);
	
	if($result == 101) exit("1077|0|0|0");
	else{
		$str="update all_api_order set oStatus=3,handleStatus=0 where pftOrder='".$_REQUEST['Ordern']."' limit 1";
		$GLOBALS['le']->query($str);
		exit("200|0|0|0");
	}
	
}


// function logfile($txt,$logname){
	// $fp = fopen('/var/www/html/new/d/logs/api/ChangYouTong/'.$logname.date('Y-m-d').'.txt',"a");
	// flock($fp, LOCK_EX) ;
	// fwrite($fp,date('Y-m-d H:i:s').'|'.$txt."\n");
	// flock($fp, LOCK_UN);
	// fclose($fp); 
// }
function logfile($word,$path){
	$filepath= '/var/www/html/new/d/logs/api/ChangYouTong/'.$path.'.txt';
	if(file_exists($filepath)){
		$word = date("Y-m-d H:i:s")."|".$word;
		$word = mb_convert_encoding($word, 'GBK', 'UTF-8');
		$maxsize  = 1024*1024*10;
		$filesize = filesize($filepath);
		$filetype = end(explode(".",$filepath));
		$filename = substr($filepath,0,strpos($filepath,'.'));
		if($filesize>$maxsize)
		{
			rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
			$handler=fopen($filepath,'w');
			flock($handler, LOCK_EX) ;
			fwrite($handler,"\n".$word);
			flock($handler, LOCK_UN);
			fclose($handler);
		}
		else
		{
			$fp = fopen('/var/www/html/new/d/logs/api/ChangYouTong/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}else{
		$fp = fopen('/var/www/html/new/d/logs/api/ChangYouTong/'.$path.'.txt',"a");
		flock($fp, LOCK_EX);
		fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
}
?>